<?php

declare (strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Model;

/**
 * @property int $id 
 * @property string $name 
 * @property int $size 
 * @property string $url 
 * @property string $md5 
 * @property int $width 
 * @property int $height 
 * @property string $store_type 
 * @property string $media_type
 * @property \Carbon\Carbon $created_at 
 * @property string $status 
 * @property int $category_id 
 */
class File extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'file';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'size' => 'integer', 'width' => 'integer', 'height' => 'integer', 'created_at' => 'datetime', 'category_id' => 'integer'];
    public function getList($where = [], $field = '*', $limit = 12)
    {
        return $this->where($where)->select($field)->orderByDesc('updated_at')->orderByDesc('id')->paginate($limit);
    }

    /**
     * 根据id获取单条信息
     * @param $where
     */
    public function getOne($where=[],$field="*")
    {
        $info = $this->where($where)->select($field)->first();
        if(empty($info)) return false;

        if(!empty($info)){
            return $info->toArray();
        }
    }

    public function add($data)
    {
        return $this->insertGetId($data);
    }
    /**
     * 删除
     * @param $id
     */
    public function del($ids)
    {
        return $this->whereIn('id',$ids)->update(['status'=>0]);
    }

    public function updateOne($where,$data)
    {
        return $this->where($where)->update($data);
    }

    public function category()
    {
        return $this->belongsToMany(FileCategory::class,'file_file_category');
        //return $this->belongsTo(FileCategory::class,'category_id','id');
    }
}